<?php
/**
 * 开放平台接口调用
 * User: dxk
 * Date: 2016/12/9
 */

namespace App\Http\Controllers\OpenApi\App;

use App\Http\Controllers\Controller;
use App\Library\ThirdParty\DingTalk\DingTalkSendMsg;
use App\Service\OpenApi\AppApi\ApiRequestCheckService;

class AppApiController extends Controller
{
    /**
     * 单个调用接口
     * @return false|string
     * @throws \Exception
     */
    public function index()
    {
        header('Content-Type: application/json');
        // 接口验证文件
        $request = file_get_contents('php://input');
        $params = json_decode($request, true);
        easy_log('api_log' . DIRECTORY_SEPARATOR . 'request', $params);
        if (empty($params)) {
            $params = request()->post();
        }
        $ApiRequestCheckService = new ApiRequestCheckService();
        // 接口权限验证
        $check_res = $ApiRequestCheckService->checkParam($params);
        if ($check_res !== true) {
            return $ApiRequestCheckService->jsonAlert($check_res['code'], $check_res['msg'], $check_res['data']);
        }
        $data = get_object_vars($ApiRequestCheckService);
        $ServiceClass = $ApiRequestCheckService->service_class;
        $action = $ApiRequestCheckService->a;
        try {
            $res = call_user_func([new $ServiceClass($data), $action]);
        } catch (\Exception $e) {
            $data = [
                'url' => request()->fullUrl(),
                'request' => $params,
            ];
            DingTalkSendMsg::sendErrorMsg($data, $e);
            return $ApiRequestCheckService->jsonAlert(500, '服务异常');
        }
        if (!isset($res['code']) || !is_numeric($res['code'])) {
            return $ApiRequestCheckService->jsonAlert(500, '接口返回数据格式错误', $res);
        } else {
            return $ApiRequestCheckService->jsonAlert($res['code'], $res['msg'], $res['data']);
        }
    }

    /**
     * 批量调用
     * @return false|string
     * @throws \Exception
     */
    public function batch()
    {
        $ApiRequestCheckService = new ApiRequestCheckService();
        $requests = file_get_contents('php://input');
        $requests = json_decode($requests, true);
        if (!is_array($requests)) {
            return $ApiRequestCheckService->batchJsonAlert(10001, '无法解析的request请求', [], $requests);
        }
        if (empty($requests)) {
            return $ApiRequestCheckService->batchJsonAlert(10001, '空的request请求', [], $requests);
        }
        // 接口验证文件
        $results = [];
        foreach ($requests as $k => $request) {
            $res = $ApiRequestCheckService->checkParam($request);
            if ($res !== true) {
                $results[$k] = $res;
                continue;
            }
            $data = get_object_vars($ApiRequestCheckService);
            $ServiceClass = $ApiRequestCheckService->service_class;
            $action = $ApiRequestCheckService->a;
            try {
                $res = call_user_func([new $ServiceClass($data), $action]);
            } catch (\Exception $e) {
                $res = ['code' => 500, 'msg' => '服务异常'];
                $data = [
                    'url' => request()->fullUrl(),
                    'request' => $request,
                ];
                DingTalkSendMsg::sendErrorMsg($data, $e);
            }
            if (!isset($res['code']) || !is_numeric($res['code'])) {
                $results[$k] = alert_info(500, '接口返回数据格式错误', $res);
            } else {
                $results[$k] = $res;
            }
        }
        return $ApiRequestCheckService->batchJsonAlert(0, '批量请求成功', $results, $requests);
    }
}
